﻿/*  Windows Powerplan Switcher
    Copyright (C) 2011 Tom Schreiber / Codebit
 
    http://codebit.de/windows-power-plan-switcher
    mailto: tom.schreiber@codebit.de

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Codebit.PowerPlanSwitcher
{
    class WinPowerPlanHandler
    {
        public List<PowerScheme> powerSchemes;

        /// <summary>
        /// Initialize the handler and updates the list of the Power-Schemes
        /// </summary>
        public WinPowerPlanHandler()
        {
            powerSchemes = new List<PowerScheme>();
            Update();            
        }

        /// <summary>
        /// Manually updates the list of the Power-Schemes
        /// </summary>
        public void Update()
        {
            try
            {
                // Get Power-Schemes with 'powercfg'
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.FileName = "powercfg";
                process.StartInfo.Arguments = "-list";
                process.Start();

                // Read output / wait
                string output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();

                // Initialize RegEx
                // sample: "Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c  (High performance)"
                Regex rx = new Regex("GUID:\\s(?<guid>[^\\s]+)\\s+\\((?<name>.+)\r\n");

                // Get RegEx-Matches
                powerSchemes.Clear();
                foreach(Match match in rx.Matches(output))
                {
                    // get GUID and Name of Plan
                    string guid = match.Result("${guid}");
                    string name = match.Result("${name}");

                    // is plan active / cut name
                    bool active;
                    if (name.Substring(name.Length - 1, 1) == "*")
                    {
                        name = name.Substring(0, name.Length - 3);
                        active = true;
                    }
                    else
                    {
                        name = name.Substring(0, name.Length - 1);
                        active = false;
                    }

                    // Create new Scheme-Object
                    PowerScheme scheme = new PowerScheme();
                    scheme.GUID = guid;
                    scheme.Name = name;
                    scheme.Active = active;

                    powerSchemes.Add(scheme);
  
                }               
            }
            catch (Exception ex)
            {
                throw ex;
            }           
        }

        /// <summary>
        /// Switch to a specific Power-Scheme
        /// </summary>
        /// <param name="guid">The GUID of the Scheme</param>
        public void SwitchToScheme(string guid)
        {
            // TODO: Check GUID

            try
            {
                // Get Power-Schemes with 'powercfg'
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.FileName = "powercfg";
                process.StartInfo.Arguments = "-setactive  "+ guid;                
                process.Start();

                // Read output / wait
                string output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();

                // TODO: look at the output

                // Updates the List
                Update();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Return a array of all power-schemes
        /// </summary>
        public PowerScheme[] PowerSchemes
        {
            get
            {
                return powerSchemes.ToArray();
            }
        }
    }

    // Single PowerScheme-Class
    class PowerScheme
    {
        public string GUID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public bool Active { get; set; }
    }
}
